<?php

// app/Controller/UsersController.php

App::import('Model', 'Champion');
App::import('Controller', 'Champions');

class HomesController extends AppController {
    
    var $Champions;
    var $Champion;

    public function beforeFilter() {
        parent::beforeFilter();
        $this->Auth->allow('index');
        $this->Champions = new ChampionsController;
        $this->Champions->constructClasses();
        $this->Champion = new Champion();
    }

    public function index() {
        $new=$this->Champion->find('first', array('order' => array('Champion.created DESC')));
        $all=$this->Champion->find('all', array('order' => array('Champion.created DESC'),'conditions' => array('Champion.id <>' => $new['Champion']['id'])));
        $this->set('champions',$this->Champions->view($new['Champion']['id']));
        shuffle($all);
        //cont: número máximo de champions para o rotate
        $cont=0;
        $rotation=array();
        foreach($all as $item){
            if($cont==6)
                break;
            array_push($rotation,$this->Champions->view($all[$cont]['Champion']['id']));
            $cont++;
        }
        $qtd=count($rotation);
        $qtdAux=$qtd+1;
        //coluna esquerda da index (rotation)
        $qtdColEsq=(int)($qtdAux/2);
        $printRotationLeft=array();
        $printRotationRight=array();
        for($i=0;$i<$qtdColEsq;$i++){
            $str='<div class="champion">'.
                    '<a href="/prtlol/portallol/champions/view/'.$rotation[$i]['Champion']['id'].
                    '"><img src="/prtlol/portallol/img/../files/'.$rotation[$i]['Champion'][0]['file_name'].
                    '" alt="View" title="View" width="260" height="153" /></a>
                        <div class="champion_information">
					<p><a href="/prtlol/portallol/videos/view/'.$rotation[$i]['Champion']['id'].'">Lista de Game Play</a></p>
					<p>Tutoriais:<a href="#">Tutorial 1</a></p>'.
                        '</div>'.
                    '</div>';
            array_push($printRotationLeft,$str);
        }
        //coluna direita da index (rotation)
        for($i=$qtdColEsq;$i<$qtd;$i++){
            $str='<div class="champion">'.
                    '<a href="/prtlol/portallol/champions/view/'.$rotation[$i]['Champion']['id'].
                    '"><img src="/prtlol/portallol/img/../files/'.$rotation[$i]['Champion'][0]['file_name'].
                    '" alt="View" title="View" width="260" height="153" /></a>
                        <div class="champion_information">
					<p><a href="/prtlol/portallol/videos/view/'.$rotation[$i]['Champion']['id'].'">Lista de Game Play</a></p>
					<p>Tutoriais:<a href="#">Tutorial 1</a></p>'.
                        '</div>'.
                    '</div>';
            array_push($printRotationRight,$str);
        }
        $this->set('printRL',$printRotationLeft);
        $this->set('printRR',$printRotationRight);        
    }
}